<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\UserSummary;
use App\Models\UserCumulate;
use App\Models\ArticleSummary;
use App\Models\UserReadSummary;
use App\Models\UserShareSummary;
use App\Models\UpstreamMessageHourly;
use App\Models\UpstreamMessageSummary;

class SyncOADataCube extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'csw:SyncOADataCube';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    protected $app;

    /**
     * Create a new command instance.
     *
     */
    public function __construct()
    {
        parent::__construct();
        $this->app = app('wechat.official_account');
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("csw:SyncOADataCube start!");
        try {
            $this->getYestodayAll();
            //$this->sendDataCubeMail();
        } catch (\Exception $se) {
            $this->info($se->getMessage());
        }

        $this->info("csw:SyncOADataCube finished!");
    }

    protected function getYestodayAll()
    {
        $from = -1;
        $to = -1;
        $this->getUserSummary($from, $to);
        $this->getUserCumulate($from, $to);
        $this->getArticleSummary($from, $to);
        $this->getUserReadSummary($from, $to);
        $this->getUserShareSummary($from, $to);
        $this->getUpstreamMessageSummary($from, $to);
        $this->getUpstreamMessageHourly($from, $to);

    }

    // 获取用户增减数据, 最大时间跨度：7;
    protected function getUserSummary($from, $to)
    {
        for($i = $from; $i <= $to; $i = $i + 6) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $j = $i + 6;
            if($j > -1) $j = -1;
            $yestoday = date('Y-m-d', strtotime( $j .'day'));
            $this->info($preday . "\t" . $yestoday);
            $userSummary = $this->app->data_cube->userSummary($preday, $yestoday);
            if (isset($userSummary) && isset($userSummary['list'])) {
                foreach ($userSummary['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UserSummary::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date'],
                            'user_source' => $dayRow['user_source']
                        ],
                        [
                            'new_user' => $dayRow['new_user'],
                            'cancel_user' => $dayRow['cancel_user']
                        ]
                    );
                }
            }
        }
    }

    // 获取累计用户数据, 最大时间跨度：7;
    protected function getUserCumulate($from, $to)
    {
        for($i = $from; $i <= $to; $i = $i + 6) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $j = $i + 6;
            if($j > -1) $j = -1;
            $yestoday = date('Y-m-d', strtotime( $j .'day'));
            $this->info($preday . "\t" . $yestoday);
            $userCumulate = $this->app->data_cube->userCumulate($preday, $yestoday);
            if (isset($userCumulate) && isset($userCumulate['list'])) {
                foreach ($userCumulate['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UserCumulate::updateOrCreate(
                        ['ref_date' => $dayRow['ref_date']],
                        [
                            'cumulate_user' => $dayRow['cumulate_user'],  //总用户量
                        ]
                    );
                }
            }
        }
    }

    // 获取图文群发每日数据, 最大时间跨度：1;
    protected function getArticleSummary($from, $to)
    {
        for($i = $from; $i <= $to; $i++) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $this->info($preday);
            $articleSummary = $this->app->data_cube->articleSummary($preday, $preday);
            if (isset($articleSummary) && isset($articleSummary['list'])) {
                foreach ($articleSummary['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    ArticleSummary::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date'],
                            'msgid' => $dayRow['msgid'],
                        ],
                        [
                            'title' => $dayRow['title'],
                            'int_page_read_user' => $dayRow['int_page_read_user'],
                            'int_page_read_count' => $dayRow['int_page_read_count'],
                            'ori_page_read_user' => $dayRow['ori_page_read_user'],
                            'ori_page_read_count' => $dayRow['ori_page_read_count'],
                            'share_user' => $dayRow['share_user'],
                            'share_count' => $dayRow['share_count'],
                            'add_to_fav_user' => $dayRow['add_to_fav_user'],
                            'add_to_fav_count' => $dayRow['add_to_fav_count'],
                        ]
                    );
                }
            }
        }
    }

    // 获取图文统计数据, 最大时间跨度：3;
    protected function getUserReadSummary($from, $to)
    {
        for($i = $from; $i <= $to; $i = $i + 3) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $j = $i + 3;
            if($j > -1) $j = -1;
            $yestoday = date('Y-m-d', strtotime( $j .'day'));
            $this->info($preday . "\t" . $yestoday);
            $userReadSummary = $this->app->data_cube->userReadSummary($preday, $yestoday);
            if (isset($userReadSummary) && isset($userReadSummary['list'])) {
                foreach ($userReadSummary['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UserReadSummary::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date']
                        ],
                        [
                            'int_page_read_user' => $dayRow['int_page_read_user'],
                            'int_page_read_count' => $dayRow['int_page_read_count'],
                            'ori_page_read_user' => $dayRow['ori_page_read_user'],
                            'ori_page_read_count' => $dayRow['ori_page_read_count'],
                            'share_user' => $dayRow['share_user'],
                            'share_count' => $dayRow['share_count'],
                            'add_to_fav_user' => $dayRow['add_to_fav_user'],
                            'add_to_fav_count' => $dayRow['add_to_fav_count'],
                        ]
                    );
                }
            }
        }
    }

    // 获取图文分享转发数据, 最大时间跨度：7;
    protected function getUserShareSummary($from, $to)
    {
        $step = 6;
        for($i = $from; $i <= $to; $i = $i + $step) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $j = $i + $step;
            if($j > -1) $j = -1;
            $yestoday = date('Y-m-d', strtotime( $j .'day'));
            $this->info($preday . "\t" . $yestoday);
            $userShareSummary = $this->app->data_cube->userShareSummary($preday, $yestoday);
            if (isset($userShareSummary) && isset($userShareSummary['list'])) {
                foreach ($userShareSummary['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UserShareSummary::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date'],
                            'share_scene' => $dayRow['share_scene'],
                        ],
                        [
                            'share_count' => $dayRow['share_count'],
                            'share_user' => $dayRow['share_user'],
                        ]
                    );
                }
            }
        }
    }

    // 获取消息发送概况数据, 最大时间跨度：7;
    protected function getUpstreamMessageSummary($from, $to)
    {
        $step = 6;
        for($i = $from; $i <= $to; $i = $i + $step) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $j = $i + $step;
            if($j > -1) $j = -1;
            $yestoday = date('Y-m-d', strtotime( $j .'day'));
            $this->info($preday . "\t" . $yestoday);
            $upstreamMessageSummary = $this->app->data_cube->upstreamMessageSummary($preday, $yestoday);
            if (isset($upstreamMessageSummary) && isset($upstreamMessageSummary['list'])) {
                foreach ($upstreamMessageSummary['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UpstreamMessageSummary::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date'],
                            'msg_type' => $dayRow['msg_type'],
                        ],
                        [
                            'msg_user' => $dayRow['msg_user'],
                            'msg_count' => $dayRow['msg_count'],
                        ]
                    );
                }
            }
        }
    }

    // 获取消息发送分时数据, 最大时间跨度：1;
    protected function getUpstreamMessageHourly($from, $to)
    {
        for($i = $from; $i <= $to; $i++) {
            $preday = date('Y-m-d', strtotime($i .' day'));
            $this->info($preday);
            $upstreamMessageHourly = $this->app->data_cube->upstreamMessageHourly($preday, $preday);
            if (isset($upstreamMessageHourly) && isset($upstreamMessageHourly['list'])) {
                foreach ($upstreamMessageHourly['list'] as $dayRow) {
                    $this->info($dayRow['ref_date']);
                    UpstreamMessageHourly::updateOrCreate(
                        [
                            'ref_date' => $dayRow['ref_date'],
                            'ref_hour' => $dayRow['ref_hour'],
                            'msg_type' => $dayRow['msg_type'],
                        ],
                        [
                            'msg_user' => $dayRow['msg_user'],
                            'msg_count' => $dayRow['msg_count'],
                        ]
                    );
                }
            }
        }
    }

    protected function sendDataCubeMail()
    {
//        $transport = (new \Swift_SmtpTransport('smtp.exmail.qq.com', 465, 'ssl'))
//            ->setUsername('chensw@gzads.com')
//            ->setPassword('Tureture#123')
//        ;
//
//        $mailer = new \Swift_Mailer($transport);
//        $message = (new \Swift_Message('公众号统计信息'))
//            ->setFrom(['chensw@gzads.com' => 'chenshengwen'])
//            ->setTo(['chensw@gzads.com'])
//            ->setBody('Here is the message itself')
//        ;
//        $result = $mailer->send($message);

        //$date = date("Y-m-d", strtotime("-1 day"));
        $date = "2018-05-13";
        $userCumulate = UserCumulate::where("ref-date", $date)->first();
        $userCumulate = UserCumulate::where('ref-data', $date)->get();
        $articleSummary = ArticleSummary::where();

    }
}
